package com.meteor.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.meteor.pojo.Comment;
import com.meteor.service.CommentService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class CommentController {

    @Resource
    private CommentService commentService;

    @Value("/image/img.png")
    private String avatar;

    @GetMapping("/comments/{blogId}")
    @Transactional
    public String comments(@PathVariable Long blogId, Model model) {
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("blog_id" , blogId);
        List<Comment> list = commentService.list(wrapper);
        model.addAttribute("comments",list);
        return "blog::commentList";
    }

    @PostMapping("/comments")
    @Transactional
    public String post(Comment comment) {
        comment.setAvatar(avatar);
        commentService.save(comment);
        return "redirect:/comments/" + comment.getBlogId();
    }
}
